package de.java.hsrm.view.bars;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.io.*;

import javax.swing.*;

import de.java.hsrm.app.DataManager;

@SuppressWarnings("serial")
public class Menubar extends JMenuBar{
	
	private DataManager dataManager;
	
	// Menu 1
	private JMenu fileMenu;
	private JMenuItem openAction;
	private JMenuItem saveAction;
	private JMenuItem exitAction;
	// Menu 2
	private JMenu helpMenu;
	private JMenuItem helpAction;
	private JMenuItem aboutAction;
	

	public Menubar(DataManager dm) {
		this.dataManager = dm;
		setOpaque(false);
		initialize();
		build();
	}


	private void build() {
		// File menu
		this.fileMenu.add(this.openAction);
		this.fileMenu.add(this.saveAction);
		this.fileMenu.add(this.exitAction);
		add(fileMenu);
		// Help menu
		this.helpMenu.add(this.helpAction);
		this.helpMenu.add(this.aboutAction);
		add(helpMenu);
	}


	private void initialize() {
		// Menu 1
		this.fileMenu = new JMenu("Datei");
		this.openAction = new JMenuItem("Open");
		this.saveAction = new JMenuItem("Save");
		this.exitAction = new JMenuItem("Exit");
		// Menu 2
		this.helpMenu = new JMenu("Hilfe");
		this.helpAction = new JMenuItem("Help");
		this.aboutAction = new JMenuItem("About");
		
		initActionMenuItems();
		
	}
	
	private void initActionMenuItems() {
		// File menu
//		this.openAction = new AbstractAction("Öffnen"){
//			{putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY,1);}
//			@Override public void actionPerformed(ActionEvent e) {
//				openFile();
//				
//			}	
//		};
//		this.saveAction = new AbstractAction("Speichern"){
//			@Override public void actionPerformed(ActionEvent e) {
//				saveFile();
//				
//			}
//		};
//		this.exitAction = new AbstractAction("Beenden"){
//			@Override public void actionPerformed(ActionEvent e) {
//				System.exit(0);
//				
//			}
//		};
//		// Help menu
//		this.helpAction = new AbstractAction("Hilfe"){
//			@Override public void actionPerformed(ActionEvent e) {
//				showHelp();
//				
//			}
//		};
//		this.aboutAction = new AbstractAction("Über uns"){
//			@Override public void actionPerformed(ActionEvent e) {
//				JOptionPane.showMessageDialog(null, "Wir sind wir");
//				
//			}
//		};
	}
	
	private void showHelp() {
		// TODO implement help	
	}
	
	private void saveFile() {
		JFileChooser fc = new JFileChooser();
		if(fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
			FileWriter out = null;
			try{
				out = new FileWriter(fc.getSelectedFile());
				this.dataManager.getMainPanel().getTextArea().write(out);
			} catch (IOException ex){
				this.dataManager.getMainPanel().getTextArea().setText(ex.getMessage());
			}
			finally {
				try {out.close();} catch (Exception ex){}
			}
		}
		
	}
	
	private void openFile(){
		JFileChooser fc = new JFileChooser();
		if(fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
			FileReader in = null;
			try{
				in = new FileReader(fc.getSelectedFile());
				this.dataManager.getMainPanel().getTextArea().read(in, null);
			} catch (IOException ex){
				this.dataManager.getMainPanel().getTextArea().setText(ex.getMessage());
			}
			finally {
				try {in.close();} catch (Exception ex){}
			}
		}
	}
	

	public JMenuBar getMenuBar(){
		return this;
	}


	public JMenu getFileMenu() {
		return fileMenu;
	}




	public JMenu getHelpMenu() {
		return helpMenu;
	}




	@Override protected void paintComponent(Graphics g){
		Graphics2D g2 = (Graphics2D) g;
		
		GradientPaint p = new GradientPaint(0,0,new Color(0x555555),
				0,10,new Color(0x333333));
		g2.setPaint(p);
		g2.fillRect(0, 0, getWidth(), getHeight()/2);
		
		p = new GradientPaint(0,10,new Color(0x222222),
				0,20,new Color(0x111111));
		g2.setPaint(p);
		g2.fillRect(0, getHeight()/2, getWidth(), getHeight()/2);
		
		super.paintComponent(g);
	}

}
